const fs = require('fs');
const FormData = require('form-data');
const fetch = require('node-fetch');

async function testUpload() {
  try {
    console.log('🚀 Testing plugin upload...');

    // 读取ZIP文件
    const zipPath = './awesome-plugin-1.0.0.zip';
    const zipBuffer = fs.readFileSync(zipPath);
    
    console.log(`📦 ZIP file size: ${zipBuffer.length} bytes`);

    // 创建FormData
    const form = new FormData();
    form.append('file', zipBuffer, {
      filename: 'awesome-plugin-1.0.0.zip',
      contentType: 'application/zip'
    });
    form.append('version', '1.0.0');
    form.append('changelog', 'Initial release of the awesome plugin');

    // 获取插件ID (假设我们知道插件ID)
    const pluginId = 'cme4cwd5j0001s8dh0fbjjfnu'; // 从之前的创建响应中获取

    // 发送请求
    const response = await fetch(`http://localhost:3002/api/v1/developer/plugins/${pluginId}/versions`, {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjbWU0Y3JlODcwMDBncjhrMXN1dGM0OHJhIiwiZW1haWwiOiJ0ZXN0QGV4YW1wbGUuY29tIiwidXNlcm5hbWUiOiJ0ZXN0X2RldiIsInJvbGUiOiJkZXZlbG9wZXIiLCJpc3MiOiJiYWktaHUtY2xvdWQiLCJhdWQiOiJiYWktaHUtYXBpIiwiaWF0IjoxNzU0NzUwMDE0LCJleHAiOjE3NTQ3NTA5MTR9.KMaSj6I4mvXQx5Xn7TuANmuuvpeXT8e5fzEb1-xUJk0',
        ...form.getHeaders()
      },
      body: form
    });

    const result = await response.text();
    console.log(`📊 Response status: ${response.status}`);
    console.log(`📄 Response body: ${result}`);

    if (response.ok) {
      console.log('✅ Upload successful!');
    } else {
      console.log('❌ Upload failed!');
    }

  } catch (error) {
    console.error('❌ Error:', error.message);
  }
}

testUpload();
